home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / strpp300.zip / STR.H < prev    next >
C/C++ Source or Header  |  1993-04-11  |  13KB  |  380 lines

  1. /* -------------------------------------------------------------------- */
  2. /* String++ Version 3.00                                       04/10/93 */
  3. /*                                                                      */
  4. /* Enhanced string class for Turbo C++/Borland C++.                     */
  5. /* Copyright 1991-1993 by Carl W. Moreland                              */
  6. /*                                                                      */
  7. /* str.h                                                                */
  8. /* -------------------------------------------------------------------- */
  9.  
  10. #ifndef _STR_H
  11. #define _STR_H
  12.  
  13. #include <string.h>
  14. #if defined (__ZTC__)
  15. #include <stream.hpp>            // Zortech
  16. #else
  17. #include <iostream.h>            // Borland
  18. #endif
  19.  
  20. const char LEFT   = 0;
  21. const char CENTER = 1;
  22. const char RIGHT  = 2;
  23. const char NOCLIP = 0;
  24. const char NOTRIM = 0;
  25. const char CLIP   = 1;
  26. const char TRIM   = 2;
  27. const char WHITESPACE = 0;
  28.  
  29. class RegExp;
  30.  
  31. class String
  32. {
  33. protected:
  34.   unsigned strLen;            // length of the String
  35.   char*    strPtr;            // pointer to the String contents
  36.   unsigned bufferLen;            // length of the buffer
  37.  
  38. public:
  39.   String();                // default constructor;
  40.   String(const char c,            // initialize with a character,
  41.          const unsigned n = 1);        //   optional # of characters
  42.   String(const char* p);        // initialize with a char *
  43.   String(const char* p,            // initialize with a char *,
  44.          const unsigned pos,        //   optional starting char
  45.          const unsigned len = 10000);    //   optional # of chars
  46.   String(const String& s);        // initialize with another String
  47.   String(const String& s,        // initialize with another String,
  48.          const unsigned pos,        //   optional starting char
  49.          const unsigned len = 10000);    //   optional # of chars
  50.   String(const int n);            // initialize with an integer
  51.   String(const long n);            // initialize with a long int
  52.   String(const float n,            // initialize with a float
  53.          const char* format = "");    //   and a format specifier
  54.   String(const double n,        // initialize with a double
  55.          const char* format = "");    //   and a format specifier
  56.  
  57.  ~String(void);
  58.  
  59. protected:
  60.   static unsigned strMinLength;        // minimum memory allocated
  61.   static unsigned strIncLength;        // incremental memory allocated
  62.   static String*  findIn;        // these are for FindFirst, etc.
  63.   static String&  findStr;
  64.   static unsigned findPos;
  65.   static String   fpFormat;
  66.  
  67.   virtual void SetStr(const char c,
  68.                       const unsigned n = 1);
  69.   virtual void SetStr(const char* p);
  70.   virtual void SetStr(const char* p,
  71.                       const unsigned pos,
  72.               const unsigned len = 10000);
  73.   virtual void SetStr(const String& s);
  74.   virtual void SetStr(const String& s,
  75.                       const unsigned pos,
  76.               const unsigned len = 10000);
  77.   virtual void AddStr(const char c);
  78.   virtual void AddStr(const char* p);
  79.   virtual void AddStr(const String& s);
  80.   virtual unsigned GetSize(unsigned n);
  81.   void ltos(const long n);
  82.   void dtos(const double n, const char* format);
  83.  
  84. public:
  85.   void SetMinLength(unsigned len = 16) const { strMinLength = len; }
  86.   void SetIncLength(unsigned len = 8)  const { strIncLength = len; }
  87.   unsigned SetSize(unsigned len);
  88.   void Minimize(void);                // minimize bufferLen
  89.   void SetFloatFormat(const char*) const;     // floating point format
  90.  
  91.   virtual operator const char() const        { return strPtr[0]; }
  92.   virtual operator const char*() const       { return strPtr; }
  93.   virtual const char  operator *() const     { return strPtr[0]; }
  94.   virtual const char* operator()() const     { return strPtr; }
  95.   const char* operator()(unsigned pos) const { return strPtr+pos; }
  96.   String      operator()(unsigned pos, unsigned len) const;
  97.   char* ptr(void) const                      { return strPtr; }
  98.   char* ptr(unsigned pos) const              { return strPtr+pos; }
  99.  
  100.   virtual int Length(void) const { return strLen; }
  101.   virtual int Len(void)    const { return strLen; }
  102.   String& toUpper(void);            // convert to uppercase
  103.   String& toLower(void);            // convert to lowercase
  104.   int&    Value(int& n) const;            // int value of str
  105.   long&   Value(long& n) const;            // long int value of str
  106.   float&  Value(float& n) const;        // float value of str
  107.   double& Value(double& n) const;        // double value of str
  108.  
  109.   String& Left(unsigned len);            // left   len chars
  110.   String& Right(unsigned len);            // right  len chars
  111.   String& Mid(unsigned pos,            // middle len chars from pos
  112.               unsigned len);
  113.   String& Justify(char type,            // justify str
  114.                   unsigned len,
  115.           char mode = CLIP|TRIM);
  116.   String& Trim(int mode = CENTER,        // delete whitespace
  117.                char ch = WHITESPACE);
  118.  
  119.   String& Insert(unsigned pos,            // insert substring
  120.                  const String& s);
  121.   String& Delete(unsigned pos,            // delete substring
  122.                  unsigned len = 1);
  123.   String& Replace(unsigned pos,
  124.                   unsigned len,
  125.                   const String& to);
  126.   int     Replace(const String& from,        // substitute from -> to
  127.                   const String& to,
  128.               unsigned count = 10000);
  129.   int     Replace(const RegExp& from,
  130.                   const String& to,
  131.                   unsigned count = 10000);
  132.   char*   Copy(char*&) const;            // copy str to char*
  133.  
  134.   int     Index(const String& t) const;        // position of t in str
  135.   int     Index(const RegExp& t) const;        // position of t in str
  136.   String  SubStr(unsigned pos,            // substring at position pos
  137.                  unsigned len = 10000) const;
  138.   int     Split(String*& a,            // split into an array a on
  139.                 const String& fs) const;    //   field separator fs
  140.   int     Split(String*& a,            // split into an array a on
  141.                 const RegExp& fs) const;    //   field separator fs
  142.   int     Sub(const String& from,        // substitute from -> to
  143.               const String& to,
  144.           unsigned count = 10000);
  145.   int     Sub(const RegExp& from,        // substitute from -> to
  146.               const String& to,
  147.               unsigned count = 10000);
  148.  
  149.   int     FindFirst(const String& s) const;    // first occurance of s
  150.   int     FindNext (void) const;        // next occurance of s
  151.   int     FindPrev (void) const;        // previous occurance of s
  152.   int     FindLast (const String& s) const;    // last occurance of s
  153.  
  154.   virtual String& operator=(const char c);    // str1 = char
  155.   virtual String& operator=(const char* p);    // str1 = char*
  156.   virtual String& operator=(const String& s);    // str1 = str
  157.   virtual String& operator=(const int n);    // str1 = int
  158.   virtual String& operator=(const long n);    // str1 = long
  159.   virtual String& operator=(const float n);    // str1 = float
  160.   virtual String& operator=(const double n);    // str1 = double
  161.   String& operator+=(const char c);        // str1 += char
  162.   String& operator+=(const char* p);        // str1 += char*
  163.   String& operator+=(const String& s);        // str1 += str
  164.   String& operator*=(const unsigned n);        // str1 *= n
  165.   char&   operator[](const unsigned i) const;    // ch = str[i] or str[i] = ch
  166.  
  167.   friend String operator+(const String& s1, const String& s2);
  168.   friend String operator+(const String& s,  const char* p);
  169.   friend String operator+(const char* p,    const String& s);
  170.   friend String operator*(const String& s,  const unsigned n);
  171.   friend String operator*(const unsigned n, const String& s);
  172.  
  173.   virtual String& operator<<(const char c);    // s << char
  174.   virtual String& operator<<(const char* p);    // s << char*
  175.   virtual String& operator<<(const String& s);    // s << str
  176.   virtual String& operator<<(const int n);    // s << int
  177.   virtual String& operator<<(const long n);    // s << long
  178.   virtual String& operator<<(const float n);    // s << float
  179.   virtual String& operator<<(const double n);    // s << double
  180. };
  181.  
  182. typedef String string;            // compatibility with older versions
  183.  
  184. ostream& operator<<(ostream&, const String&);
  185. istream& operator>>(istream&, String&);
  186.  
  187. /* ----- Awk-style functions ------------------------------------------ */
  188.  
  189. inline int length(const String& s) { return s.Len(); }
  190. int    index(const String& s, const String& t);
  191. String substr(const String& s, unsigned pos, unsigned len = 10000);
  192. int    split(const String& s, String*& a, const String& fs);
  193. int    gsub(const String& from, const String& to,
  194.             String& str, unsigned count = 10000);
  195. inline int sub(const String& from, const String& to, String& str) {
  196.   return gsub(from, to, str, 1);
  197. }
  198.  
  199. // Regular Expression versions - requires regexp.cpp
  200.  
  201. extern int index(const String& s, const RegExp& t);
  202. extern int split(const String& s, String*& a, const RegExp& fs);
  203. extern int gsub(const RegExp& from, const String& to,
  204.                 String& str, unsigned count = 10000);
  205. inline int sub(const RegExp& from, const String& to, String& str) {
  206.   return gsub(from, to, str, 1);
  207. }
  208.  
  209. /* ----- Other C-style functions -------------------------------------- */
  210.  
  211. String toupper(const String& s);
  212. String tolower(const String& s);
  213. String left(const String& s, unsigned len);
  214. String right(const String& s, unsigned len);
  215. String mid(const String& s, unsigned pos, unsigned len);
  216. String justify(const String& s, char type, unsigned len, char mode=CLIP|TRIM);
  217. String trim(const String& s, int mode = CENTER);
  218.  
  219. /* ----- Inline functions --------------------------------------------- */
  220.  
  221. inline String& String::operator=(const int n) {
  222.   operator=((long)n);
  223.   return *this;
  224. }
  225.  
  226. inline String& String::operator=(const float n) {
  227.   operator=((double)n);
  228.   return *this;
  229. }
  230.  
  231. inline String& String::operator<<(const int n) {
  232.   operator<<((long)n);
  233.   return *this;
  234. }
  235.  
  236. inline String& String::operator<<(const float n) {
  237.   operator<<((double)n);
  238.   return *this;
  239. }
  240.  
  241. inline int String::Replace(const String& from,
  242.                            const String& to, unsigned count) {
  243.   return gsub(from, to, *this, count);
  244. }
  245.  
  246. inline int String::Replace(const RegExp& from,
  247.                            const String& to, unsigned count) {
  248.   return gsub(from, to, *this, count);
  249. }
  250.  
  251. inline int operator==(const String& s1, const String& s2) {
  252.   return strcmp(s1, s2) == 0;
  253. }
  254.  
  255. inline int operator!=(const String& s1, const String& s2) {
  256.   return strcmp(s1, s2) != 0;
  257. }
  258.  
  259. inline int operator<(const String& s1, const String& s2) {
  260.   return strcmp(s1, s2) < 0;
  261. }
  262.  
  263. inline int operator>(const String& s1, const String& s2) {
  264.   return strcmp(s1, s2) > 0;
  265. }
  266.  
  267. inline int operator<=(const String& s1, const String& s2) {
  268.   return strcmp(s1, s2) <= 0;
  269. }
  270.  
  271. inline int operator>=(const String& s1, const String& s2) {
  272.   return strcmp(s1, s2) >= 0;
  273. }
  274.  
  275. inline int operator==(const String& s, const char* p) {
  276.   return strcmp(s, p) == 0;
  277. }
  278.  
  279. inline int operator!=(const String& s, const char* p) {
  280.   return strcmp(s, p) != 0;
  281. }
  282.  
  283. inline int operator<(const String& s, const char* p) {
  284.   return strcmp(s, p) < 0;
  285. }
  286.  
  287. inline int operator>(const String& s, const char* p) {
  288.   return strcmp(s, p) > 0;
  289. }
  290.  
  291. inline int operator<=(const String& s, const char* p) {
  292.   return strcmp(s, p) <= 0;
  293. }
  294.  
  295. inline int operator>=(const String& s, const char* p) {
  296.   return strcmp(s, p) >= 0;
  297. }
  298.  
  299. inline int operator==(const char* p, const String& s) {
  300.   return strcmp(p, s) == 0;
  301. }
  302.  
  303. inline int operator!=(const char* p, const String& s) {
  304.   return strcmp(p, s) != 0;
  305. }
  306.  
  307. inline int operator<(const char* p, const String& s) {
  308.   return strcmp(p, s) < 0;
  309. }
  310.  
  311. inline int operator>(const char* p, const String& s) {
  312.   return strcmp(p, s) > 0;
  313. }
  314.  
  315. inline int operator<=(const char* p, const String& s) {
  316.   return strcmp(p, s) <= 0;
  317. }
  318.  
  319. inline int operator>=(const char* p, const String& s) {
  320.   return strcmp(p, s) >= 0;
  321. }
  322.  
  323. inline int operator==(const String& s, const char c) {
  324.   return *s == c;
  325. }
  326.  
  327. inline int operator!=(const String& s, const char c) {
  328.   return *s != c;
  329. }
  330.  
  331. inline int operator<(const String& s, const char c) {
  332.   return *s < c;
  333. }
  334.  
  335. inline int operator>(const String& s, const char c) {
  336.   return *s > c;
  337. }
  338.  
  339. inline int operator<=(const String& s, const char c) {
  340.   return *s <= c;
  341. }
  342.  
  343. inline int operator>=(const String& s, const char c) {
  344.   return *s >= c;
  345. }
  346.  
  347. inline int String::Index(const String& t) const {
  348.   return index(*this, t);
  349. }
  350.  
  351. inline int String::Index(const RegExp& t) const {
  352.   return index(*this, t);
  353. }
  354.  
  355. inline String String::SubStr(unsigned p, unsigned n) const {
  356.   return substr(*this, p, n);
  357. }
  358.  
  359. inline int String::Split(String*& a, const String& fs) const {
  360.   return split(*this, a, fs);
  361. }
  362.  
  363. inline int String::Split(String*& a, const RegExp& fs) const {
  364.   return split(*this, a, fs);
  365. }
  366.  
  367. inline int String::Sub(const String& from, const String& to, unsigned count) {
  368.   return gsub(from, to, *this, count);
  369. }
  370.  
  371. inline int String::Sub(const RegExp& from, const String& to, unsigned count) {
  372.   return gsub(from, to, *this, count);
  373. }
  374.  
  375. extern const String STR_NULL;        // a global NULL string
  376. extern const String& StrNull;        // same string, different name
  377. extern const String& Str;        // same string, different name
  378.  
  379. #endif
  380.